package leagal_aid.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import leagal_aid.util.DBUtil;

public class ContactBean {
	private int id;
	private String name;
	private String title;
	private String telephone;
	private String email;
	private String address;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	

	PreparedStatement statement = null;
	Connection connection = null;
	ResultSet rs = null;
	

	public ContactBean findAll() {
		ContactBean contactBean = new ContactBean();
		DBUtil dbUtil = new DBUtil();
		String sql = "select * from contact";
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			rs = statement.executeQuery();
			while(rs.next()) {
				contactBean.setId(rs.getInt("id"));
				contactBean.setName(rs.getString("name"));
				contactBean.setEmail(rs.getString("email"));
				contactBean.setTelephone(rs.getString("telephone"));
				contactBean.setTitle(rs.getString("title"));
				contactBean.setAddress(rs.getString("address"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return contactBean;
		
	}
	
	public boolean doUpdata(String title,String name,String telephone,String email,String address,int id) {
		boolean updata = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		//String sql = "select password from admin where username = " + username + "'";
		String sql = "update contact set title=?,name=?,telephone=?,email=?,address=? where id=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, title);
			statement.setString(2, name);
			statement.setString(3, telephone);
			statement.setString(4, email);
			statement.setString(5, address);
			statement.setInt(6, id);
			System.out.println(updata);
			int i = statement.executeUpdate();
			if (i>0) {
				updata = true;
			}
				
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return updata;
		
	}
}
